/**
 * 
 */
package com.great.meal.search.hibernate;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.great.meal.search.dao.GenericDao;

/**
 * 实现对数据的增加 删除 更新操作的父类.
 * 
 * @author lenovo
 *
 */
public class GenericDaoImpl<T> extends HibernateDaoSupport implements
		GenericDao<T> {
	protected final Log logger = LogFactory.getLog(getClass());
	protected Class<T> entityClass;
	
	/**
	 * 获取子类的class类型
	 */
	@SuppressWarnings("unchecked")
	public GenericDaoImpl(){
		Type genType = getClass().getGenericSuperclass();  
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();  
		entityClass = (Class) params[0];  
	}
	
	@Override
	public Class<T> getTClass(){
		return entityClass;
	}
	
	@Override
	public void delete(Object o) {
		getHibernateTemplate().delete(o);
	}

	@Override
	public void deleteById(Serializable id) {
		getHibernateTemplate().delete(getById(id));
	}

	@Override
	public Collection<T> findAll(Class<T> entityClass) {
		return getHibernateTemplate().loadAll(entityClass);
	}

	@Override
	
	public T getById(Serializable id) {
		return getHibernateTemplate().get(entityClass, id);
	}

	@Override
	public void save(Object o) {
		getHibernateTemplate().save(o);
	}

	@Override
	public void saveOrUpdate(Object o) {
		getHibernateTemplate().saveOrUpdate(o);
	}

	@Override
	public void update(Object o) {
		getHibernateTemplate().update(o);
	}

	
	@Override
	public List<?> find(String queryString) {
		return getHibernateTemplate().find(queryString);
	}

	@Override
	public List<?> find(String queryString, Object value) {
		return getHibernateTemplate().find(queryString, value);
	}

	@Override
	public List<?> findByNamedQuery(String queryName) {
		return getHibernateTemplate().findByNamedQuery(queryName);
	}

	@Override
	public List<?> findByNamedQuery(String queryName, Object value) {
		return getHibernateTemplate().findByNamedQuery(queryName, value);
	}

	@Override
	public List<?> findByNamedQueryAndNamedParam(String queryName,
			String paramName, Object value) {
		return getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, 
				paramName, value);
	}

	@Override
	public List<?> findByNamedQueryAndNamedParam(String queryName,
			String[] paramNames, Object[] values) {
		return getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, paramNames, values);
	}
	
	

}
